package code.base;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;

/**
 * @author xchen
 * @since 2022-01-20
 */
public abstract class MpGeneratorTemplate {
    /**
     * 全局配置
     */
    protected abstract void globalConfig(GlobalConfig globalConfig);

    /**
     * 数据源配置
     */
    protected abstract void dataSourceConfig(DataSourceConfig dataSourceConfig);

    /**
     * 策略配置
     */
    protected abstract void strategyConfig(StrategyConfig strategyConfig);

    /**
     * 包配置
     */
    protected abstract void packageConfig(PackageConfig packageConfig);

    /**
     * 配置末班引擎
     */
    protected abstract AbstractTemplateEngine templateEngineConfig();

    /**
     * 模板配置
     */
    protected void templateConfig(TemplateConfig templateConfig) {
    }

    /**
     * 自定义配置
     */
    protected InjectionConfig injectionConfig() {
        return null;
    }

    public void execute() {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig(globalConfig);
        generator.setGlobalConfig(globalConfig);
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig(dataSourceConfig);
        generator.setDataSource(dataSourceConfig);
        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig(packageConfig);
        generator.setPackageInfo(packageConfig);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig(strategyConfig);
        generator.setStrategy(strategyConfig);
        //模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig(templateConfig);
        generator.setTemplate(templateConfig);
        //模板引擎
        AbstractTemplateEngine templateEngine = templateEngineConfig();
        generator.setTemplateEngine(templateEngine);
        //自定义配置
        InjectionConfig injectionConfig = injectionConfig();
        generator.setCfg(injectionConfig);

        generator.execute();

    }
}
